
<!DOCTYPE html>
<html lang="zh-cn">
    
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="UTF-8" >
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="generator" content="XGHeaven&#39;s Blog">
    <title>命令行构造工具之 Yargs 详细介绍 - XGHeaven&#39;s Blog</title>
    <meta name="author" content="XGHeaven">
    
    
    
        <link rel="alternate" type="application/atom+xml" title="RSS" href="/atom.xml">
    
    <meta name="description" content="命令行工具构造工具之 yargs简介话说 yargs 是什么呢？简单来说，就是用来解析 cli 参数的。话不多说，我们来简单了解一下。 光速开始.argv 一切的开始，简单的不要不要的使用这个最简单的方式就是直接调用 .argv 这个 getter，他会自动对 process.argv 进行解析。并返回解析后的对象。 123// argv.jsconst argv = require(&apos;yarg">
<meta name="keywords" content="Node.js,CLI,yargs">
<meta property="og:type" content="blog">
<meta property="og:title" content="命令行构造工具之 Yargs 详细介绍">
<meta property="og:url" content="http://blog.xgheaven.cn/2017/05/03/cli-build-tools-for-yargs/index.html">
<meta property="og:site_name" content="XGHeaven&#39;s Blog">
<meta property="og:description" content="命令行工具构造工具之 yargs简介话说 yargs 是什么呢？简单来说，就是用来解析 cli 参数的。话不多说，我们来简单了解一下。 光速开始.argv 一切的开始，简单的不要不要的使用这个最简单的方式就是直接调用 .argv 这个 getter，他会自动对 process.argv 进行解析。并返回解析后的对象。 123// argv.jsconst argv = require(&apos;yarg">
<meta property="og:locale" content="zh-cn">
<meta property="og:updated_time" content="2017-05-04T09:51:02.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="命令行构造工具之 Yargs 详细介绍">
<meta name="twitter:description" content="命令行工具构造工具之 yargs简介话说 yargs 是什么呢？简单来说，就是用来解析 cli 参数的。话不多说，我们来简单了解一下。 光速开始.argv 一切的开始，简单的不要不要的使用这个最简单的方式就是直接调用 .argv 这个 getter，他会自动对 process.argv 进行解析。并返回解析后的对象。 123// argv.jsconst argv = require(&apos;yarg">
<meta name="twitter:creator" content="@XGHeaven">
    
    
        
    
    
        <meta property="og:image" content="https://www.gravatar.com/avatar/f34474afe04544ffbe944a804f0ae762?s=640"/>
    
    
    
    
    <!--STYLES-->
    <link rel="stylesheet" href="/assets/css/style-mhhgzztqkaub4zd4cl8bd83f7mgh9j6njnhilft4hamhrjsliqyzwo2cfzdk.min.css">
    <!--STYLES END-->
    
    <script type="text/javascript">
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-71388235-1']);
        _gaq.push(['_trackPageview']);
        (function() {
            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
        })();
    </script>

    
    <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?9894361f828b64144ac1f2ac0c58c300";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
    </script>

</head>

    <body>
        <div id="blog">
            <!-- Define author's picture -->


    
        
    

<header id="header" data-behavior="3">
    <i id="btn-open-sidebar" class="fa fa-lg fa-bars"></i>
    <h1 class="header-title">
        <a class="header-title-link" href="/ ">XGHeaven&#39;s Blog</a>
    </h1>
    
        
            <a  class="header-right-picture "
                href="#about">
        
        
            <img class="header-picture" src="https://www.gravatar.com/avatar/f34474afe04544ffbe944a804f0ae762?s=90"/>
        
        </a>
    
</header>
            <!-- Define author's picture -->


    

<nav id="sidebar" data-behavior="3">
    
        <div class="sidebar-profile">
            <a href="/#about">
                    <img class="sidebar-profile-picture" src="https://www.gravatar.com/avatar/f34474afe04544ffbe944a804f0ae762?s=110"/>
            </a>
            <span class="sidebar-profile-name">XGHeaven</span>
        </div>
    
    
        <ul class="sidebar-buttons">
        
            <li class="sidebar-button">
                
                    <a  class="sidebar-button-link "
                         href="/ "
                        
                    >
                
                    <i class="sidebar-button-icon fa fa-lg fa-home"></i>
                    <span class="sidebar-button-desc">首页</span>
                </a>
        </li>
        
            <li class="sidebar-button">
                
                    <a  class="sidebar-button-link "
                         href="/all-categories"
                        
                    >
                
                    <i class="sidebar-button-icon fa fa-lg fa-bookmark"></i>
                    <span class="sidebar-button-desc">分类</span>
                </a>
        </li>
        
            <li class="sidebar-button">
                
                    <a  class="sidebar-button-link "
                         href="/all-tags"
                        
                    >
                
                    <i class="sidebar-button-icon fa fa-lg fa-tags"></i>
                    <span class="sidebar-button-desc">标签</span>
                </a>
        </li>
        
            <li class="sidebar-button">
                
                    <a  class="sidebar-button-link "
                         href="/all-archives"
                        
                    >
                
                    <i class="sidebar-button-icon fa fa-lg fa-archive"></i>
                    <span class="sidebar-button-desc">归档</span>
                </a>
        </li>
        
            <li class="sidebar-button">
                
                    <a  class="sidebar-button-link "
                         href="#about"
                        
                    >
                
                    <i class="sidebar-button-icon fa fa-lg fa-question"></i>
                    <span class="sidebar-button-desc">关于</span>
                </a>
        </li>
        
            <li class="sidebar-button">
                
                    <a  class="sidebar-button-link "
                         href="/friend-link"
                        
                    >
                
                    <i class="sidebar-button-icon fa fa-lg fa-link"></i>
                    <span class="sidebar-button-desc">友情链接</span>
                </a>
        </li>
        
    </ul>
    
        <ul class="sidebar-buttons">
        
            <li class="sidebar-button">
                
                    <a  class="sidebar-button-link " href="https://github.com/XGHeaven" target="_blank">
                
                    <i class="sidebar-button-icon fa fa-lg fa-github"></i>
                    <span class="sidebar-button-desc">GitHub</span>
                </a>
        </li>
        
            <li class="sidebar-button">
                
                    <a  class="sidebar-button-link " href="https://twitter.com/xgheaven" target="_blank">
                
                    <i class="sidebar-button-icon fa fa-lg fa-twitter"></i>
                    <span class="sidebar-button-desc">Twitter</span>
                </a>
        </li>
        
            <li class="sidebar-button">
                
                    <a  class="sidebar-button-link " href="https://facebook.com/xgheaven" target="_blank">
                
                    <i class="sidebar-button-icon fa fa-lg fa-facebook"></i>
                    <span class="sidebar-button-desc">Facebook</span>
                </a>
        </li>
        
            <li class="sidebar-button">
                
                    <a  class="sidebar-button-link " href="https://www.linkedin.com/xgheaven/" target="_blank">
                
                    <i class="sidebar-button-icon fa fa-lg fa-linkedin"></i>
                    <span class="sidebar-button-desc">LinkedIn</span>
                </a>
        </li>
        
            <li class="sidebar-button">
                
                    <a  class="sidebar-button-link " href="mailto:xgheaven@gmail.com" target="_blank">
                
                    <i class="sidebar-button-icon fa fa-lg fa-envelope-o"></i>
                    <span class="sidebar-button-desc">邮箱</span>
                </a>
        </li>
        
    </ul>
    
        <ul class="sidebar-buttons">
        
            <li class="sidebar-button">
                
                    <a  class="sidebar-button-link "
                         href="/atom.xml"
                        
                    >
                
                    <i class="sidebar-button-icon fa fa-lg fa-rss"></i>
                    <span class="sidebar-button-desc">RSS</span>
                </a>
        </li>
        
            <li class="sidebar-button">
                
                    <a  class="sidebar-button-link "
                         href="/changelog"
                        
                    >
                
                    <i class="sidebar-button-icon fa fa-lg fa-line-chart"></i>
                    <span class="sidebar-button-desc">Changelog</span>
                </a>
        </li>
        
    </ul>
    
</nav>

            
            <div id="main" data-behavior="3"
                 class="
                        hasCoverMetaIn
                        ">
                
<article class="post" itemscope itemType="http://schema.org/BlogPosting">
    
    
        <div class="post-header main-content-wrap text-left">
    
        <h1 class="post-title" itemprop="headline">
            命令行构造工具之 Yargs 详细介绍
        </h1>
    
    <div class="post-meta">
    <time itemprop="datePublished" content="Wed May 03 2017 18:55:43 GMT+0800">
	
		    5月 03, 2017
    	
    </time>
    
        <span>发布在 </span>
        
    <a class="category-link" href="/categories/Node-js/">Node.js</a>


    
</div>
</div>
    
    <div class="post-content markdown" itemprop="articleBody">
        <div class="main-content-wrap">
            <h1 id="table-of-contents">目录</h1><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#命令行工具构造工具之-yargs"><span class="toc-text">命令行工具构造工具之 yargs</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#简介"><span class="toc-text">简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#光速开始"><span class="toc-text">光速开始</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#argv-一切的开始，简单的不要不要的"><span class="toc-text">.argv 一切的开始，简单的不要不要的</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#普通参数"><span class="toc-text">普通参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#简写参数"><span class="toc-text">简写参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#全写参数"><span class="toc-text">全写参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#结果合并"><span class="toc-text">结果合并</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#我就要你在我的身边，-demandOption-key-msg"><span class="toc-text">我就要你在我的身边，.demandOption(key, msg)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#啥？你嫌我太长？还是太短-alias"><span class="toc-text">啥？你嫌我太长？还是太短:).alias</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#你要我怎样我就怎样，-boolean-array-number-count-choices"><span class="toc-text">你要我怎样我就怎样，.boolean .array .number .count .choices</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#array-key"><span class="toc-text">.array(key)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#boolean-key"><span class="toc-text">.boolean(key)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#number-key"><span class="toc-text">.number(key)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#count-key"><span class="toc-text">.count(key)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#choices-key-list"><span class="toc-text">.choices(key, list)</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#听说你和别人有千丝万缕的关系-conflicts-implies"><span class="toc-text">听说你和别人有千丝万缕的关系:( .conflicts .implies</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#可以"><span class="toc-text">可以</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#大家在一起吧-option-options"><span class="toc-text">大家在一起吧 :) .option .options</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#有用但是很简单其余参数"><span class="toc-text">有用但是很简单其余参数</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#小弟来了-command"><span class="toc-text">小弟来了 (-_-) .command</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#这个位置是你的，别人抢不走-arg1-lt-arg2-gt"><span class="toc-text">这个位置是你的，别人抢不走 [arg1] <arg2></span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#默认命令"><span class="toc-text">默认命令 *</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#方便一点-commandDir"><span class="toc-text">方便一点 .commandDir</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#从别的地方来-config-env-coerce"><span class="toc-text">从别的地方来 .config .env .coerce</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#总结"><span class="toc-text">总结</span></a></li></ol></li></ol>
<h1 id="命令行工具构造工具之-yargs"><a href="#命令行工具构造工具之-yargs" class="headerlink" title="命令行工具构造工具之 yargs"></a>命令行工具构造工具之 yargs</h1><h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>话说 yargs 是什么呢？简单来说，就是用来解析 cli 参数的。话不多说，我们来简单了解一下。</p>
<h2 id="光速开始"><a href="#光速开始" class="headerlink" title="光速开始"></a>光速开始</h2><h3 id="argv-一切的开始，简单的不要不要的"><a href="#argv-一切的开始，简单的不要不要的" class="headerlink" title=".argv 一切的开始，简单的不要不要的"></a>.argv 一切的开始，简单的不要不要的</h3><p>使用这个最简单的方式就是直接调用 <code>.argv</code> 这个 getter，他会自动对 <code>process.argv</code> 进行解析。并返回解析后的对象。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// argv.js</span></div><div class="line"><span class="keyword">const</span> argv = <span class="built_in">require</span>(<span class="string">'yargs'</span>).argv;</div><div class="line"><span class="built_in">console</span>.dir(argv)</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node argv.js -v --version --name whos</span></div><div class="line">&#123; _: [], v: <span class="literal">true</span>, version: <span class="literal">true</span>, name: <span class="string">'whos'</span>, <span class="string">'$0'</span>: <span class="string">'argv.js'</span> &#125;</div></pre></td></tr></table></figure>
<p>什么缩写，什么参数，统统搞定。是不是 so easy。</p>
<p>在默认情况下，所有的参数只有三种值，<code>Boolean</code>、<code>String</code>、<code>Array&lt;Boolean|String</code>。</p>
<p>而且 <code>$0</code> 代表当前脚本的名称，这个就不多讲了</p>
<p>参数后面可以使用空格或者 <code>=</code> 。例如 <code>-d=ok —name=bill</code></p>
<h4 id="普通参数"><a href="#普通参数" class="headerlink" title="普通参数"></a>普通参数</h4><p>如果参数没有 <code>-</code> 开头，那么将它放入 <code>_</code> ，简称为普通参数</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node argv.js a b c</span></div><div class="line">&#123; _: [ <span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span> ], <span class="string">'$0'</span>: <span class="string">'argv.js'</span> &#125;</div></pre></td></tr></table></figure>
<h4 id="简写参数"><a href="#简写参数" class="headerlink" title="简写参数"></a>简写参数</h4><p>如果参数只有一个 <code>-</code> 开头，那么后面的参数为缩写参数，缩写参数的值默认设置成 <code>true</code>。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node argv.js -a -b -c</span></div><div class="line">&#123; _: [], a: <span class="literal">true</span>, b: <span class="literal">true</span>, c: <span class="literal">true</span>, <span class="string">'$0'</span>: <span class="string">'argv.js'</span> &#125;</div></pre></td></tr></table></figure>
<p>同时，<code>yargs</code> 支持将缩写参数合并在一起书写。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node argv.js -abc</span></div><div class="line">&#123; _: [], a: <span class="literal">true</span>, b: <span class="literal">true</span>, c: <span class="literal">true</span>, <span class="string">'$0'</span>: <span class="string">'argv.js'</span> &#125;</div></pre></td></tr></table></figure>
<p>效果和上面是一样的。</p>
<p>如果缩写参数后面跟着普通参数，那么缩写参数的值就会自动设置成普通参数的值而不再是 <code>true</code>。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node argv.js -a haha -b lala -c hehe</span></div><div class="line">&#123; _: [], a: <span class="string">'haha'</span>, b: <span class="string">'lala'</span>, c: <span class="string">'hehe'</span>, <span class="string">'$0'</span>: <span class="string">'argv.js'</span> &#125;</div></pre></td></tr></table></figure>
<p>那么有人问了，如果我这样写会怎么样？ <code>-abc hahaha</code>，let’s try.</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node argv.js -abc hahaha</span></div><div class="line">&#123; _: [], a: <span class="literal">true</span>, b: <span class="literal">true</span>, c: <span class="string">'hahaha'</span>, <span class="string">'$0'</span>: <span class="string">'argv.js'</span> &#125;</div></pre></td></tr></table></figure>
<p>结果显示，其实就和</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node argv.js -a -b -c hahaha</span></div></pre></td></tr></table></figure>
<p>是一样的，可以见得，代码中其实就是将 <code>-abc</code> 拆成了 <code>-a -b -c</code> 进行解析的。</p>
<h4 id="全写参数"><a href="#全写参数" class="headerlink" title="全写参数"></a>全写参数</h4><p>除去上面两种参数，就剩下全写参数（不要吐槽为啥叫全写参数，因为实在是不知道该叫什么名字）</p>
<p>全写参数和缩写参数差不多，只不过他不能合并成一个书写，其他都是一样的</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node argv.js --version --laugh haha</span></div><div class="line">&#123; _: [], version: <span class="literal">true</span>, laugh: <span class="string">'haha'</span>, <span class="string">'$0'</span>: <span class="string">'argv.js'</span> &#125;</div></pre></td></tr></table></figure>
<h4 id="结果合并"><a href="#结果合并" class="headerlink" title="结果合并"></a>结果合并</h4><p>作为一个好 Module ，怎么会没有考虑到下面这种奇葩情况呢？</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node argv.js -a -a -a -a -a -a -a</span></div></pre></td></tr></table></figure>
<p>大家猜猜会是什么结果 :) 此处略过 10000 秒。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">&#123; _: [],</div><div class="line">  a: [ <span class="literal">true</span>, <span class="literal">true</span>, <span class="literal">true</span>, <span class="literal">true</span>, <span class="literal">true</span>, <span class="literal">true</span>, <span class="literal">true</span> ],</div><div class="line">  <span class="string">'$0'</span>: <span class="string">'argv.js'</span> &#125;</div></pre></td></tr></table></figure>
<p>没错，<code>yargs</code> 将每一个参数单独处理，然后最后合并成了一个数组，是不是很有意思，也就是说你可以写出下面的东东。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node argv.js --fuck whose --fuck your --fuck daddy --fuck</span></div><div class="line">&#123; _: [],</div><div class="line">  fuck: [ <span class="string">'whose'</span>, <span class="string">'your'</span>, <span class="string">'daddy'</span>, <span class="literal">true</span> ],</div><div class="line">  <span class="string">'$0'</span>: <span class="string">'argv.js'</span> &#125;</div></pre></td></tr></table></figure>
<p>最简单的模式，也是最有趣的模式，值得去玩。</p>
<h3 id="我就要你在我的身边，-demandOption-key-msg"><a href="#我就要你在我的身边，-demandOption-key-msg" class="headerlink" title="我就要你在我的身边，.demandOption(key, msg)"></a>我就要你在我的身边，<code>.demandOption(key, msg)</code></h3><p>如果你需要某个参数一定存在，这怎么办呢？难道要自己手动 <code>if</code> 一下，那真的好蠢啊。</p>
<p><code>.demandOption</code> 就是这么来了</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// demand.js</span></div><div class="line"><span class="keyword">const</span> argv = <span class="built_in">require</span>(<span class="string">'yargs'</span>).demandOption(<span class="string">'baby'</span>).argv</div><div class="line"><span class="built_in">console</span>.dir(argv)</div></pre></td></tr></table></figure>
<p><code>baby</code> 在，世界一切太平，不管他是怎么在我的身边的。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node demand.js --baby</span></div><div class="line">&#123; _: [], baby: <span class="literal">true</span>, <span class="string">'$0'</span>: <span class="string">'demand.js'</span> &#125;</div><div class="line"><span class="comment"># node demand.js --baby I</span></div><div class="line">&#123; _: [], baby: <span class="string">'I'</span>, <span class="string">'$0'</span>: <span class="string">'demand.js'</span> &#125;</div><div class="line"><span class="comment"># node demand.js --baby --baby --baby --baby I</span></div><div class="line">&#123; _: [], baby: [ <span class="literal">true</span>, <span class="literal">true</span>, <span class="literal">true</span>, <span class="string">'I'</span> ], <span class="string">'$0'</span>: <span class="string">'demand.js'</span> &#125;</div></pre></td></tr></table></figure>
<p><code>baby</code> 不在，世界爆炸(exit code != 0)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node demand.js</span></div><div class="line">Options:</div><div class="line">  --baby                                     [required]</div><div class="line"></div><div class="line">Missing required argument: baby</div></pre></td></tr></table></figure>
<p><code>.demandOption(key, msg)</code> 的 <code>key</code> 支持数组和字符串，分别表示单个和多个 <code>required</code> 的参数。而第二个参数值在没有满足条件的时候显示的文字。</p>
<h3 id="啥？你嫌我太长？还是太短-alias"><a href="#啥？你嫌我太长？还是太短-alias" class="headerlink" title="啥？你嫌我太长？还是太短:).alias"></a>啥？你嫌我太长？还是太短:)<code>.alias</code></h3><blockquote>
<p> 俗话说的好，参数太长怎么办，变短一点喽</p>
</blockquote>
<p>其实是我自己说的，可以给一个命令取一个别名，不管是变长还是变短，都很简单。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// alias.js</span></div><div class="line"><span class="keyword">const</span> argv = <span class="built_in">require</span>(<span class="string">'yargs'</span>).alias(<span class="string">'long'</span>, [<span class="string">'l'</span>, <span class="string">'lo'</span>]).alias(<span class="string">'short'</span>, <span class="string">'s'</span>).argv</div><div class="line"><span class="built_in">console</span>.dir(argv)</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node alias.js -l --long --lo -s --short</span></div><div class="line">&#123; _: [],</div><div class="line">  l: [ <span class="literal">true</span>, <span class="literal">true</span>, <span class="literal">true</span> ],</div><div class="line">  long: [ <span class="literal">true</span>, <span class="literal">true</span>, <span class="literal">true</span> ],</div><div class="line">  lo: [ <span class="literal">true</span>, <span class="literal">true</span>, <span class="literal">true</span> ],</div><div class="line">  s: [ <span class="literal">true</span>, <span class="literal">true</span> ],</div><div class="line">  short: [ <span class="literal">true</span>, <span class="literal">true</span> ],</div><div class="line">  <span class="string">'$0'</span>: <span class="string">'alias.js'</span> &#125;</div></pre></td></tr></table></figure>
<p>可以看到 <code>l</code> <code>lo</code> <code>long</code> 是一样的，<code>s</code> <code>short</code> 是一样的，可长可短，自由随意。</p>
<h3 id="你要我怎样我就怎样，-boolean-array-number-count-choices"><a href="#你要我怎样我就怎样，-boolean-array-number-count-choices" class="headerlink" title="你要我怎样我就怎样，.boolean .array .number .count .choices"></a>你要我怎样我就怎样，<code>.boolean</code> <code>.array</code> <code>.number</code> <code>.count</code> <code>.choices</code></h3><p>有的时候，需要某些参数是固定的格式，而不是其他的方式，那么就需要这些方法来描述一个参数的类型。这些参数对于 <code>alias</code> 之后的参数同样也是可以的。</p>
<h4 id="array-key"><a href="#array-key" class="headerlink" title=".array(key)"></a><code>.array(key)</code></h4><p>顾名思义，直接将参数的类型设置为数组，他会将后面所有的非普通参数作为当前参数的值。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// array.js</span></div><div class="line"><span class="keyword">const</span> argv = <span class="built_in">require</span>(<span class="string">'yargs'</span>).array(<span class="string">'girls'</span>).argv</div><div class="line"><span class="built_in">console</span>.dir(argv)</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node array.js --girls Abby Aimee --stop --girls Alisa Angelia Amanda</span></div><div class="line">&#123; _: [],</div><div class="line">  girls: [ <span class="string">'Abby'</span>, <span class="string">'Aimee'</span>, <span class="string">'Alisa'</span>, <span class="string">'Angelia'</span>, <span class="string">'Amanda'</span> ],</div><div class="line">  stop: <span class="literal">true</span>,</div><div class="line">  <span class="string">'$0'</span>: <span class="string">'array.js'</span> &#125;</div></pre></td></tr></table></figure>
<h4 id="boolean-key"><a href="#boolean-key" class="headerlink" title=".boolean(key)"></a><code>.boolean(key)</code></h4><p>将参数类型设置为 <code>Boolean</code> 类型。如果后面的类型不是 <code>Boolean</code> 类型(<code>true</code>、<code>false</code>)，那么将不会设置为当前参数的值，并且当有多个的时候，不会合并成数组。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// boolean.js</span></div><div class="line"><span class="keyword">const</span> argv = <span class="built_in">require</span>(<span class="string">'yargs'</span>).boolean(<span class="string">'love'</span>).argv</div><div class="line"><span class="built_in">console</span>.dir(argv)</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node boolean.js I --love you and --love again</span></div><div class="line">&#123; _: [ <span class="string">'I'</span>, <span class="string">'you'</span>, <span class="string">'and'</span>, <span class="string">'again'</span> ],</div><div class="line">  love: <span class="literal">true</span>,</div><div class="line">  <span class="string">'$0'</span>: <span class="string">'boolean.js'</span> &#125;</div></pre></td></tr></table></figure>
<h4 id="number-key"><a href="#number-key" class="headerlink" title=".number(key)"></a><code>.number(key)</code></h4><p>将参数类型设置为 <code>Number</code> 类型。基本规则如下：</p>
<ol>
<li>如果没有填写值，那么默认是 <code>undefined</code></li>
<li>如果设置的值不合法，那么是 <code>NaN</code></li>
<li>否则是格式化为数字，使用 <code>Number</code> 构造方法</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// number.js</span></div><div class="line"><span class="keyword">const</span> argv = <span class="built_in">require</span>(<span class="string">'yargs'</span>).number([<span class="string">'bust'</span>, <span class="string">'waist'</span>, <span class="string">'hips'</span>, <span class="string">'height'</span>]).argv</div><div class="line"><span class="built_in">console</span>.dir(argv)</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node number.js --bust --waist 24 --hips zz</span></div><div class="line">&#123; _: [], bust: undefined, waist: 24, hips: NaN, <span class="string">'$0'</span>: <span class="string">'number.js'</span> &#125;</div></pre></td></tr></table></figure>
<h4 id="count-key"><a href="#count-key" class="headerlink" title=".count(key)"></a><code>.count(key)</code></h4><p>统计一下这个参数被使用了多少次，使用 <code>.count</code> 之后，参数默认就变成了 <code>Boolean</code> 类型，但是只统计他出现的次数。经常用来作为设置 debug 的输出级别。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// count.js</span></div><div class="line"><span class="keyword">const</span> argv = <span class="built_in">require</span>(<span class="string">'yargs'</span>).count(<span class="string">'v'</span>).count(<span class="string">'people'</span>).argv</div><div class="line"><span class="built_in">console</span>.log(argv)</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node count.js -v -vv --people --people false</span></div><div class="line">&#123; _: [], v: 3, people: 2, <span class="string">'$0'</span>: <span class="string">'count.js'</span> &#125;</div></pre></td></tr></table></figure>
<h4 id="choices-key-list"><a href="#choices-key-list" class="headerlink" title=".choices(key, list)"></a><code>.choices(key, list)</code></h4><p>设置某个参数只能为某些值，可以和<code>number</code> <code>boolean</code> <code>count</code> 组合。</p>
<p>其本质是 <code>indexOf</code> 操作，也就是 <code>===</code> 做比较操作，所以这也就是为啥 <code>array</code> 不能和他匹配的原因。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// choices</span></div><div class="line"><span class="keyword">const</span> argv = <span class="built_in">require</span>(<span class="string">'yargs'</span>)</div><div class="line">	.choices(<span class="string">'look'</span>, [<span class="string">'beatuify'</span>, <span class="string">'oh, god'</span>])</div><div class="line">	.choices(<span class="string">'time'</span>, [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]).number(<span class="string">'time'</span>)</div><div class="line">	.choices(<span class="string">'many'</span>, [<span class="number">1</span>,<span class="number">2</span>]).count(<span class="string">'many'</span>)</div><div class="line">	.argv</div><div class="line"><span class="built_in">console</span>.dir(argv)</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node choices.js --look "oh, god"</span></div><div class="line">&#123; _: [], look: <span class="string">'oh, god'</span>, <span class="string">'$0'</span>: <span class="string">'choices.js'</span> &#125;</div><div class="line"></div><div class="line"><span class="comment"># node choices.js --look no</span></div><div class="line">Invalid values:</div><div class="line">  Argument: look, Given: <span class="string">"no"</span>, Choices: <span class="string">"beatuify"</span>, <span class="string">"oh, god"</span></div><div class="line">  </div><div class="line"><span class="comment"># node choices.js --time 1</span></div><div class="line">&#123; _: [], time: 1, <span class="string">'$0'</span>: <span class="string">'choices.js'</span> &#125;</div><div class="line"></div><div class="line"><span class="comment"># node choices.js --time 5</span></div><div class="line">Invalid values:</div><div class="line">  Argument: time, Given: 5, Choices: 1, 2, 3, 4</div><div class="line"> </div><div class="line"><span class="comment"># node choices.js --many --many</span></div><div class="line">&#123; _: [], many: 2, <span class="string">'$0'</span>: <span class="string">'choices.js'</span> &#125;</div><div class="line"></div><div class="line"><span class="comment"># node choices.js --many --many --many</span></div><div class="line">Invalid values:</div><div class="line">  Argument: many, Given: 3, Choices: 1, 2</div></pre></td></tr></table></figure>
<h3 id="听说你和别人有千丝万缕的关系-conflicts-implies"><a href="#听说你和别人有千丝万缕的关系-conflicts-implies" class="headerlink" title="听说你和别人有千丝万缕的关系:( .conflicts .implies"></a>听说你和别人有千丝万缕的关系:( <code>.conflicts</code> <code>.implies</code></h3><p>简单一说：</p>
<ul>
<li><code>.implies(我, 她)</code> 有我先有她，有她不一定有我</li>
<li><code>.confilcts(我, 他)</code> 有我没他，有他没我</li>
</ul>
<p>如果两个都存在在一个参数上面的时候，<code>implies</code> 优先级会更高。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// imcon.js</span></div><div class="line"><span class="keyword">const</span> argv = <span class="built_in">require</span>(<span class="string">'yargs'</span>)</div><div class="line">	.conflicts(<span class="string">'me'</span>, <span class="string">'him'</span>)</div><div class="line">	.implies(<span class="string">'me'</span>, <span class="string">'her'</span>)</div><div class="line">	.argv</div><div class="line"><span class="built_in">console</span>.dir(argv)</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node imcon.js --me --him --her</span></div><div class="line">Arguments me and him are mutually exclusive</div><div class="line"></div><div class="line"><span class="comment"># implies 有更高的优先级</span></div><div class="line"><span class="comment"># node imcon.js --me --him</span></div><div class="line">Implications failed:</div><div class="line">  me -&gt; her</div><div class="line">  </div><div class="line"><span class="comment"># node imcon.js --me</span></div><div class="line">Implications failed:</div><div class="line">  me -&gt; her</div><div class="line">  </div><div class="line"><span class="comment"># node imcon.js --me --her</span></div><div class="line">&#123; _: [], me: <span class="literal">true</span>, her: <span class="literal">true</span>, <span class="string">'$0'</span>: <span class="string">'imcon.js'</span> &#125;</div><div class="line"></div><div class="line"><span class="comment"># node imcon.js --him --her</span></div><div class="line">&#123; _: [], him: <span class="literal">true</span>, her: <span class="literal">true</span>, <span class="string">'$0'</span>: <span class="string">'imcon.js'</span> &#125;</div></pre></td></tr></table></figure>
<h3 id="可以"><a href="#可以" class="headerlink" title="可以"></a>可以</h3><h3 id="大家在一起吧-option-options"><a href="#大家在一起吧-option-options" class="headerlink" title="大家在一起吧 :) .option .options"></a>大家在一起吧 :) <code>.option</code> <code>.options</code></h3><p>其实就是将上面的的所有的命令合并成一个 object，里面的 <code>key</code> 就是对应的函数名，而值就是参数。只不过 <code>.options</code> 是很多 <code>.option</code> 的集合。</p>
<p>这个就请看官网的<a href="http://yargs.js.org/docs/#methods-optionskey-opt" target="_blank" rel="external">例子</a>和<a href="https://github.com/yargs/yargs/blob/master/yargs.js#L176" target="_blank" rel="external">源码</a></p>
<h4 id="有用但是很简单其余参数"><a href="#有用但是很简单其余参数" class="headerlink" title="有用但是很简单其余参数"></a>有用但是很简单其余参数</h4><ul>
<li><code>.default</code> <code>.defaults</code> 设置默认参数值</li>
<li><code>.describe</code> 对参数的描述</li>
<li><code>.usage</code> 设置命令的提示的使用方法</li>
<li><code>.help</code> 设置帮助的指令，添加 <code>--help</code> ，但是没有 <code>-h</code> ，需要手动添加，可以选择是否添加 <code>help</code> 子命令</li>
<li><code>.group</code> 分组，比如可以设置启动参数为一组，停止参数为一组，只是看起来比较舒服一些，并不影响什么内容。</li>
<li><code>.normalize</code> 对参数的值用 <code>path.normalize</code> </li>
<li><code>.version</code> 添加版本显示参数 <code>--version</code>，不过不添加缩写参数</li>
<li><code>.wrap</code> 设置信息输出的最大的列宽度，比如说 <code>--help</code> 显示帮助参数。<code>.wrap(null)</code> 取消列宽限制，<code>.wrap(require(&#39;yargs&#39;).terminalWidth())</code> 设置为当前宽度。默认是 <code>Math.min(80, windowWidth</code></li>
</ul>
<h3 id="小弟来了-command"><a href="#小弟来了-command" class="headerlink" title="小弟来了 (-_-) .command"></a>小弟来了 (-_-) <code>.command</code></h3><p>最简单的就是想实现类似 git 的那样的带有子命令的命令行操作，那么就需要这个东西。</p>
<p>他有如下的参数：</p>
<ul>
<li><code>.command(cmd, desc, [builder], [handler])</code></li>
<li><code>.command(cmd, desc, [module])</code></li>
<li><code>.command(module)</code></li>
<li><code>builder</code> 是构造器，可以是 <code>Object|yargs =&gt; {}</code>，如果是对象，那么和 <code>.options</code> 是一样的。如果是函数，参数是 <code>yargs</code> 可以通过上面的函数添加参数。</li>
<li><code>handler</code> 是处理器，当解析完成后，传入解析的结果，此时可以对结果进行处理。</li>
<li><code>module</code> 最简单了，就是有<ul>
<li><code>command</code> 命令名</li>
<li><code>aliases</code> 别名</li>
<li><code>describe</code> 描述</li>
<li><code>builder</code> 构造器</li>
<li><code>handler</code> 处理器</li>
</ul>
</li>
</ul>
<p>当匹配到一个命令的时候， <code>yargs</code> 会做如下处理：</p>
<ol>
<li>把当前命令输入到当前作用域中</li>
<li>清空所有的非全局的配置</li>
<li>如果传入了 <code>builder</code>，就通过其设置当前命令</li>
<li>解析和验证参数</li>
<li>如何一切正常，那么运行 <code>handle</code>，如果传入了的话</li>
<li>从当前作用域中弹出</li>
</ol>
<h4 id="这个位置是你的，别人抢不走-arg1-lt-arg2-gt"><a href="#这个位置是你的，别人抢不走-arg1-lt-arg2-gt" class="headerlink" title="这个位置是你的，别人抢不走 [arg1] &lt;arg2&gt;"></a>这个位置是你的，别人抢不走 <code>[arg1] &lt;arg2&gt;</code></h4><p>有的时候希望命令必须要接受一个参数，或者接受一个可选参数，那么可以对命令使用 <code>&lt;&gt;</code> 和 <code>[]</code> 设置他的位置。<code>&lt;&gt;</code> 表示这个命令必须要有，<code>[]</code> 表示这个参数可选。</p>
<p>有如下规则：</p>
<ul>
<li>通过 <code>|</code> 设置别名，例如 <code>[name|username]</code> ，在最后的解析中，<code>name</code> 和 <code>username</code> 是一样的。</li>
<li>最后一个可选参数支持添加 <code>…</code> 变成可变参数，例如 <code>downloadto &lt;from&gt; [to…]</code> 那么 <code>to</code> 是一个数组，并且必须要是命令中的最后一个可选参数才能变成可变参数。</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// like.js</span></div><div class="line"><span class="keyword">const</span> argv = <span class="built_in">require</span>(<span class="string">'yargs'</span>)</div><div class="line">	.command(<span class="string">'like &lt;who&gt;'</span>, <span class="string">'you like who'</span>, &#123;&#125;, arg =&gt; <span class="built_in">console</span>.dir(arg))</div><div class="line">	.command(<span class="string">'dislike [who]'</span>, <span class="string">'you dislike who'</span>, &#123;&#125;, arg =&gt; <span class="built_in">console</span>.dir(arg))</div><div class="line">	.argv</div><div class="line"><span class="built_in">console</span>.dir(argv)</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node like.js like you</span></div><div class="line">&#123; _: [ <span class="string">'like'</span> ], <span class="string">'$0'</span>: <span class="string">'like.js'</span>, who: <span class="string">'you'</span> &#125;</div><div class="line">&#123; _: [ <span class="string">'like'</span> ], <span class="string">'$0'</span>: <span class="string">'like.js'</span>, who: <span class="string">'you'</span> &#125;</div><div class="line"></div><div class="line"><span class="comment"># node like.js like</span></div><div class="line">like.js like &lt;who&gt;</div><div class="line"></div><div class="line">Not enough non-option arguments: got 0, need at least 1</div><div class="line"></div><div class="line"><span class="comment"># node like.js dislike</span></div><div class="line">&#123; _: [ <span class="string">'dislike'</span> ], <span class="string">'$0'</span>: <span class="string">'like.js'</span> &#125;</div><div class="line">&#123; _: [ <span class="string">'dislike'</span> ], <span class="string">'$0'</span>: <span class="string">'like.js'</span> &#125;</div><div class="line"></div><div class="line"><span class="comment"># node like.js dislike you</span></div><div class="line">&#123; _: [ <span class="string">'dislike'</span> ], <span class="string">'$0'</span>: <span class="string">'like.js'</span>, who: <span class="string">'you'</span> &#125;</div><div class="line">&#123; _: [ <span class="string">'dislike'</span> ], <span class="string">'$0'</span>: <span class="string">'like.js'</span>, who: <span class="string">'you'</span> &#125;</div></pre></td></tr></table></figure>
<h4 id="默认命令"><a href="#默认命令" class="headerlink" title="默认命令 *"></a>默认命令 <code>*</code></h4><p>有的时候当没有任何命令匹配到的时候，希望有一个默认匹配的，那么可以用 <code>*</code> 代替普通命令的位置。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// defaultCommand.js</span></div><div class="line"><span class="keyword">const</span> argv = <span class="built_in">require</span>(<span class="string">'yargs'</span>)</div><div class="line">	.command(<span class="string">'*'</span>, <span class="string">'default command'</span>, &#123;&#125;, () =&gt; <span class="built_in">console</span>.log(<span class="string">'called command'</span>))</div><div class="line">	.argv</div><div class="line"><span class="built_in">console</span>.dir(argv)</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># node defaultCommand.js --name</span></div><div class="line">called <span class="built_in">command</span></div><div class="line">&#123; _: [], name: <span class="literal">true</span>, <span class="string">'$0'</span>: <span class="string">'defaultCommand.js'</span> &#125;</div></pre></td></tr></table></figure>
<h4 id="方便一点-commandDir"><a href="#方便一点-commandDir" class="headerlink" title="方便一点 .commandDir"></a>方便一点 <code>.commandDir</code></h4><p>表示直接从文件夹中动态加载命令。详情请参考<a href="http://yargs.js.org/docs/#methods-commanddirdirectory-opts" target="_blank" rel="external">文档</a></p>
<h3 id="从别的地方来-config-env-coerce"><a href="#从别的地方来-config-env-coerce" class="headerlink" title="从别的地方来 .config .env .coerce"></a>从别的地方来 <code>.config</code> <code>.env</code> <code>.coerce</code></h3><p>写到这里，作者累了，所以：</p>
<ul>
<li><code>.config</code> 动态的从命令行中接受一个 <code>json</code> 文件路径，并自动加载。 <a href="http://yargs.js.org/docs/#methods-configobject" target="_blank" rel="external">doc</a> </li>
<li><code>.env</code> 设置环境变量的前缀，自动将这些前缀的环境变量去掉前缀，使用小驼峰和下划线方式加载。<a href="http://yargs.js.org/docs/#methods-envprefix" target="_blank" rel="external">doc</a></li>
<li><code>.coerce</code> 获取到变量值之后转化成别的值。<a href="http://yargs.js.org/docs/#methods-coercekey-fn" target="_blank" rel="external">doc</a></li>
</ul>
<p>还有很多细节的，不过我觉得文档挺详细的，我就不多说了。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>感觉还是不错的，接口很简单，也通俗易懂。相比 <code>commander</code> 是两种不同的风格。<code>commander</code> 上手简单，但是前置知识有一些，而 <code>yargs</code> 相比前置知识的要求比较少，而且更加灵活。</p>

            
                

            
        </div>
    </div>
    <div id="post-footer" class="post-footer main-content-wrap">
        
            <div class="post-footer-tags">
                <span class="text-color-light text-small">标签</span><br/>
                
    <a class="tag tag--primary tag--small t-link" href="/tags/CLI/">CLI</a> <a class="tag tag--primary tag--small t-link" href="/tags/Node-js/">Node.js</a> <a class="tag tag--primary tag--small t-link" href="/tags/yargs/">yargs</a>

            </div>
        
        <div class="post-actions-wrap">
    <nav>
        <ul class="post-actions post-action-nav">
            <li class="post-action">
                
                    <a class="post-action-btn btn btn--default tooltip--top" href="/2017/06/09/all-in-one-timer/"  data-tooltip="All in One - Timer">
                
                    <i class="fa fa-angle-left"></i>
                    <span class="hide-xs hide-sm text-small icon-ml">上一篇</span>
                </a>
            </li>
            <li class="post-action">
                
                    <a class="post-action-btn btn btn--default tooltip--top" href="/2017/02/06/how-to-live-in-macos/" data-tooltip="如何在 Macos 系统中开启直播~适用于支持 RTMP 协议的直播">
                
                    <span class="hide-xs hide-sm text-small icon-mr">下一篇</span>
                    <i class="fa fa-angle-right"></i>
                </a>
            </li>
        </ul>
    </nav>
    <ul class="post-actions post-action-share">
        <li class="post-action hide-lg hide-md hide-sm">
            <a class="post-action-btn btn btn--default btn-open-shareoptions"  href="#btn-open-shareoptions">
                <i class="fa fa-share-alt"></i>
            </a>
        </li>
        <li class="post-action hide-xs">
            <a class="post-action-btn btn btn--default" target="new" href="https://plus.google.com/share?url=http://blog.xgheaven.cn/2017/05/03/cli-build-tools-for-yargs/">
                <i class="fa fa-google-plus"></i>
            </a>
        </li>
        <li class="post-action hide-xs">
            <a class="post-action-btn btn btn--default" target="new" href="https://www.facebook.com/sharer/sharer.php?u=http://blog.xgheaven.cn/2017/05/03/cli-build-tools-for-yargs/">
                <i class="fa fa-facebook-official"></i>
            </a>
        </li>
        <li class="post-action hide-xs">
            <a class="post-action-btn btn btn--default" target="new" href="https://twitter.com/intent/tweet?text=http://blog.xgheaven.cn/2017/05/03/cli-build-tools-for-yargs/">
                <i class="fa fa-twitter"></i>
            </a>
        </li>
        
            <li class="post-action">
                <a class="post-action-btn btn btn--default" href="#disqus_thread">
                    <i class="fa fa-comment-o"></i>
                </a>
            </li>
        
        <li class="post-action">
            
                <a class="post-action-btn btn btn--default" href="#table-of-contents">
            
                <i class="fa fa-list"></i>
            </a>
        </li>
    </ul>
</div>


        
            
                <div id="disqus_thread">
    <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
            
        
    </div>
</article>



                <footer id="footer" class="main-content-wrap">
    <span class="copyrights">
        Copyrights &copy; 2018 XGHeaven. All Rights Reserved.
    </span>
</footer>

            </div>
            
                <div id="bottom-bar" class="post-bottom-bar" data-behavior="3">
                    <div class="post-actions-wrap">
    <nav>
        <ul class="post-actions post-action-nav">
            <li class="post-action">
                
                    <a class="post-action-btn btn btn--default tooltip--top" href="/2017/06/09/all-in-one-timer/"  data-tooltip="All in One - Timer">
                
                    <i class="fa fa-angle-left"></i>
                    <span class="hide-xs hide-sm text-small icon-ml">上一篇</span>
                </a>
            </li>
            <li class="post-action">
                
                    <a class="post-action-btn btn btn--default tooltip--top" href="/2017/02/06/how-to-live-in-macos/" data-tooltip="如何在 Macos 系统中开启直播~适用于支持 RTMP 协议的直播">
                
                    <span class="hide-xs hide-sm text-small icon-mr">下一篇</span>
                    <i class="fa fa-angle-right"></i>
                </a>
            </li>
        </ul>
    </nav>
    <ul class="post-actions post-action-share">
        <li class="post-action hide-lg hide-md hide-sm">
            <a class="post-action-btn btn btn--default btn-open-shareoptions"  href="#btn-open-shareoptions">
                <i class="fa fa-share-alt"></i>
            </a>
        </li>
        <li class="post-action hide-xs">
            <a class="post-action-btn btn btn--default" target="new" href="https://plus.google.com/share?url=http://blog.xgheaven.cn/2017/05/03/cli-build-tools-for-yargs/">
                <i class="fa fa-google-plus"></i>
            </a>
        </li>
        <li class="post-action hide-xs">
            <a class="post-action-btn btn btn--default" target="new" href="https://www.facebook.com/sharer/sharer.php?u=http://blog.xgheaven.cn/2017/05/03/cli-build-tools-for-yargs/">
                <i class="fa fa-facebook-official"></i>
            </a>
        </li>
        <li class="post-action hide-xs">
            <a class="post-action-btn btn btn--default" target="new" href="https://twitter.com/intent/tweet?text=http://blog.xgheaven.cn/2017/05/03/cli-build-tools-for-yargs/">
                <i class="fa fa-twitter"></i>
            </a>
        </li>
        
            <li class="post-action">
                <a class="post-action-btn btn btn--default" href="#disqus_thread">
                    <i class="fa fa-comment-o"></i>
                </a>
            </li>
        
        <li class="post-action">
            
                <a class="post-action-btn btn btn--default" href="#table-of-contents">
            
                <i class="fa fa-list"></i>
            </a>
        </li>
    </ul>
</div>


                </div>
                <div id="share-options-bar" class="share-options-bar" data-behavior="3">
    <ul class="share-options">
        <li class="share-option">
            <a class="share-option-btn" target="new" href="https://plus.google.com/share?url=http://blog.xgheaven.cn/2017/05/03/cli-build-tools-for-yargs/">
                <i class="fa fa-google-plus"></i><span class="">分享到 Google+</span>
            </a>
        </li>
        <li class="share-option">
            <a class="share-option-btn" target="new" href="https://www.facebook.com/sharer/sharer.php?u=http://blog.xgheaven.cn/2017/05/03/cli-build-tools-for-yargs/">
                <i class="fa fa-facebook-official"></i><span>分享到 Facebook</span>
            </a>
        </li>
        <li class="share-option">
            <a class="share-option-btn" target="new" href="https://twitter.com/intent/tweet?text=http://blog.xgheaven.cn/2017/05/03/cli-build-tools-for-yargs/">
                <i class="fa fa-twitter"></i><span>分享到 Twitter</span>
            </a>
        </li>
    </ul>
</div>
<div id="share-options-mask" class="share-options-mask"></div>
            
        </div>
        


    

<div id="about">
    <div id="about-card">
        <div id="about-btn-close">
            <i class="fa fa-remove"></i>
        </div>
        
            <img id="about-card-picture" src="https://www.gravatar.com/avatar/f34474afe04544ffbe944a804f0ae762?s=110"/>
        
            <h4 id="about-card-name">XGHeaven</h4>
        
            <h5 id="about-card-bio"><p>一个弱弱的码农</p>
</h5>
        
        
            <h5 id="about-card-job">
                <i class="fa fa-briefcase"></i>
                <br/>
                <p>杭州电子科技大学学生一枚</p>

            </h5>
        
        
            <h5 id="about-card-location">
                <i class="fa fa-map-marker"></i>
                <br/>
                Weifang Shandong, China
            </h5>
        
    </div>
</div>

        
<div id="cover" style="background-image:url('/static/images/20170217-TombRaider.jpg');"></div>
    </body>
    <!--SCRIPTS-->
<script src="/assets/js/scrip-z6xcdnzggiy56kzp83ux5nnbwra1acrauxruz3kdi3u5xladb6jh4n3ylebm.min.js"></script>
<!--SCRIPTS END-->

    
        <script>
             var disqus_config = function () {
                 this.page.url = 'http://blog.xgheaven.cn/2017/05/03/cli-build-tools-for-yargs/';
                 
                    this.page.identifier = '2017/05/03/cli-build-tools-for-yargs/';
                                  
             };
            (function() {
                var d = document, s = d.createElement('script');
                var disqus_shortname = 'xgheaven-blog';
                s.src = 'https://' + disqus_shortname + '.disqus.com/embed.js';

                s.setAttribute('data-timestamp', +new Date());
                (d.head || d.body).appendChild(s);
            })();
        </script>
    



</html>
